<?php
/**
 * 采集记录控制器
 */

namespace app\api\controller;

use app\api\exception\ApiServiceException;
use app\api\service\TypeLogService;
use app\common\validate\TypeLogValidate;
use hg\apidoc\annotation as Apidoc;
use think\response\Json;

/**
 * @Apidoc\Title("采购员上传价格")
 * @Apidoc\Group("index")
 */
class TypeLogController extends ApiBaseController {
	/**
	 * 列表
	 * @param TypeLogService $service
	 * @return Json
	 */
	public function index(TypeLogService $service): Json {
		try {
			$data = $service->getList($this->param, $this->page, $this->limit);
			$result = [
				'type_log' => $data,
			];

			return api_success($result);
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}
	/**
	 * @Apidoc\Title("上传")
	 * @Apidoc\Desc("上传")
	 * @Apidoc\Method("POST")
	 * @Apidoc\Url("/api/type_log/update")
	 * @Apidoc\Param("type_id", type="string", desc="分类"),
	 * @Apidoc\Param("price", type="string", desc="价格"),
	 * @Apidoc\Param("type_log_id", type="string", desc="type_log_id"),
	 * @Apidoc\Returned("data",        type="array",                 desc="data")
	 */
	public function update(TypeLogValidate $validate, TypeLogService $service): Json {
		$this->param['user_id'] = $this->uid;
		$check = $validate->scene('api_add')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}
		if ($this->param['type_log_id'] == 0) {
			$result = $service->createData($this->param);
		} else {
			$result = $service->updateData($this->param['type_log_id'], $this->param);
		}

		return $result ? api_success() : api_error();
	}
	/**
	 * 添加
	 * @param TypeLogValidate $validate
	 * @param TypeLogService $service
	 * @return Json
	 */
	public function add(TypeLogValidate $validate, TypeLogService $service): Json {
		$check = $validate->scene('api_add')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		$result = $service->createData($this->param);

		return $result ? api_success() : api_error();
	}

	/**
	 * 详情
	 * @param TypeLogValidate $validate
	 * @param TypeLogService $service
	 * @return Json
	 */
	public function info(TypeLogValidate $validate, TypeLogService $service): Json {
		$check = $validate->scene('api_info')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {

			$result = $service->getDataInfo($this->id);
			return api_success([
				'type_log' => $result,
			]);

		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 修改
	 * @param TypeLogService $service
	 * @param TypeLogValidate $validate
	 * @return Json
	 */
	public function edit(TypeLogService $service, TypeLogValidate $validate): Json {
		$check = $validate->scene('api_edit')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->updateData($this->id, $this->param);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 删除
	 * @param TypeLogService $service
	 * @param TypeLogValidate $validate
	 * @return Json
	 */
	public function del(TypeLogService $service, TypeLogValidate $validate): Json {
		$check = $validate->scene('api_del')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->deleteData($this->id);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

}
